package com.example.demo.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public  class RedisUtil implements ApplicationContextAware {


    private static RedisTemplate redisTemplate;

    public static   RedisTemplate get(){
        return redisTemplate;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        redisTemplate=(RedisTemplate)applicationContext.getBean("redisTemplate");

    }

    public static Boolean exists(String key) {
        return (Boolean)redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.exists(key.getBytes()));
    }
}